package com.lifq.flink.source

import org.apache.flink.streaming.api.functions.source.SourceFunction

/**
 * @Author: LiFuqiang
 * @Date: 2022/9/16 15:35
 * @Version 1.0 
 * @description
 */
class DataGenerator extends SourceFunction[String] {
  var isRunning = true
  var data = "0,0,0,1,1,1,1,2,2,2,5,5,5,5,6,5,5,5,6,6,3,4,2,1,5,5,0,2,2,0,0,0,1,1,1,1,2,2,2,5,5,5,5,6,5,5,5,6,6,3,4,2,1,5,5,0,2,2".split(",")

  override def run(sourceContext: SourceFunction.SourceContext[String]): Unit = {
    val length = data.length
    var cusor = 0
    while (isRunning) {
      sourceContext.collect(data(cusor))
      cusor += 1
      Thread.sleep(1000)
      if (cusor == length) {
        isRunning = false
      }
    }
  }

  override def cancel(): Unit = {
    isRunning = false
  }
}

object DataGenerator {
  def main(args: Array[String]): Unit = {
    val numeric = Math.random() * 100
    val ns = new Array[Double](100)
    var cusor = 0
    var dd = true
    while (dd) {
      var a = Math.random() * 100
      print(a + ",")
      cusor = cusor + 1
      if (cusor == 99) {
        dd = false
      }
    }

  }
}
